package com.edgclub.edg.activity;


import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.android.volley.VolleyError;
import com.edgclub.edg.R;
import com.edgclub.edg.api.ResultCode;
import com.edgclub.edg.api.UserApi;
import com.edgclub.edg.api.param.UserParam;
import com.edgclub.edg.http.VolleyListener;
import com.edgclub.edg.utils.util.CryptUtil;
import com.edgclub.edg.utils.util.JSONHelper;
import com.edgclub.edg.utils.util.StringUtils;
import com.edgclub.edg.utils.util.UserPrefUtils;

public class SettingChangePasswordActivity extends GzBaseTooolBarActivity implements View.OnClickListener ,VolleyListener{
    private EditText old_password , new_password , new_password1;
    private UserPrefUtils userPrefUtils;
    private boolean isload=false;


    @Override
    public int getContentView() {
        return R.layout.activity_change_password;
    }

    @Override
    public void setTitleBarView() {
        setCustomActionBar();
        title.setText("修改密码");
        main_back.setVisibility(View.VISIBLE);
        main_back.setOnClickListener(this);
        right_layout.setVisibility(View.VISIBLE);
        right_text.setText("确定");
        right_layout.setOnClickListener(this);
    }

    @Override
    public void initView() {
        old_password = (EditText) findViewById(R.id.old_password);
        new_password = (EditText) findViewById(R.id.new_password);
        new_password1 = (EditText) findViewById(R.id.new_password1);
        main_back.setOnClickListener(this);
        right_layout.setOnClickListener(this);
    }

    @Override
    public void initData() {
        userPrefUtils = new UserPrefUtils(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.main_back:
                finish();
                break;
            case R.id.right_layout:
                if ("".equals(old_password.getText().toString().trim())){
                    Toast.makeText(SettingChangePasswordActivity.this,"请输入密码",Toast.LENGTH_SHORT).show();
                    return;
                }
                if ("".equals(new_password.getText().toString().trim())){
                    Toast.makeText(SettingChangePasswordActivity.this,"请输入新密码",Toast.LENGTH_SHORT).show();
                    return;
                }
                if ("".equals(new_password1.getText().toString().trim())){
                    Toast.makeText(SettingChangePasswordActivity.this,"请再次输入新密码",Toast.LENGTH_SHORT).show();
                    return;
                }

                if (!new_password.getText().toString().trim().equals(new_password1.getText().toString().trim())){
                    Toast.makeText(SettingChangePasswordActivity.this,"两次密码输入不一致",Toast.LENGTH_SHORT).show();
                    return;
                }
                if (new_password.length() < 8 || new_password.length() > 16) {
                    Toast.makeText(this, StringUtils.getString(R.string.enterpassword2), Toast.LENGTH_SHORT).show();
                    return;
                }

                upPassword();

                break;


        }
    }
    private void upPassword(){
        if(isload)return;
        UserParam param = new UserParam();
        param.setUserId(userPrefUtils.getUser().getId());
        param.setOldPwd(CryptUtil.md5(old_password.getText().toString().trim()));
        param.setNewPwd(CryptUtil.md5(new_password1.getText().toString().trim()));
        new UserApi(this).getpassword(param,this);
    }
    @Override
    public void onStart(String tag) {
        isload=true;
        showDialog("请求中，请稍后...",false);
    }

    @Override
    public void onSuccess(String response, String tag) {
        dismissDialog();
        int code = JSONHelper.getResultCode(response);
        isload=false;
        if (JSONHelper.isSuccess(response)){
            userPrefUtils.savePSW(CryptUtil.md5(new_password1.getText().toString().trim()));
            Toast.makeText(SettingChangePasswordActivity.this,"修改成功",Toast.LENGTH_SHORT).show();
            finish();
        }
        if (code==3009){
            Toast.makeText(this,"密码错误，请重新输入",Toast.LENGTH_SHORT).show();
        }else if (code==4002){
            Toast.makeText(this,"新密码与原密码不能相同",Toast.LENGTH_SHORT).show();
        }
    }
    @Override
    public void onFaile(VolleyError volleyError, String tag) {
        isload=false;
        dismissDialog();
        ResultCode.toastVolleyError(SettingChangePasswordActivity.this, volleyError);
    }

}
